import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ydad -o ydad.json
https://rhoxjr.avnyg6.lat/cn/home/web/
'''


class YdadSpider(scrapy.Spider):
    name = "ydad"
    # 前缀
    prefix = 'https://rhoxjr.'
    # 中缀
    website = 'avnyg6'
    # 后缀
    suffix = '.lat/'
    allowed_domains = ['rhoxjr.' + website + '.lat']
    start_urls = [prefix + website + suffix + 'cn/home/web/index.php']

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_url = get_video_url_one(content)
        tag_list = response.xpath("//div[@class='info']/div[@class='item']/a/text()").extract()
        if len(video_url) and len(tag_list):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags=tag_list[0], url=response.url, name='', pUrl='', vUrl=format_url_one(video_url[0]))
        url_list = response.xpath("//div[@class='item ']/a/@href").extract()
        title_list = response.xpath("//div[@class='item ']/a/@title").extract()
        pic_list = response.xpath("//div[@class='item ']/a/div[@class='img']/img/@data-original").extract()
        if len(url_list) and len(title_list) and len(pic_list):
            for index, value in enumerate(url_list):
                self.i = self.i + 1
                yield get_video_item(id=self.i, url=split_joint(self.prefix + self.website + self.suffix, url_list[index]), name=title_list[index], pUrl=pic_list[index])
        # 从网页中提取url链接
        url_list = get_url(content)
        # 提取url
        for url in url_list:
            if url.startswith('/') or url.startswith('html'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
            elif url.startswith('http') or url.startswith('rhoxjr'):
                yield scrapy.Request(url, callback=self.parse)
